package com.bristua.mall.user.biz;

import com.bristua.mall.common.utils.CacheUtil;
import com.bristua.mall.common.utils.DateUtil;
import com.bristua.mall.common.utils.SequenceUtil;
import com.bristua.mall.user.dao.TUserAdressMapper;
import com.bristua.mall.user.model.TUser;
import com.bristua.mall.user.model.TUserAdress;
import com.bristua.mall.user.model.condition.TUserAdressExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2019/3/24.
 */
@Service
@Transactional
public class AddUserAddressBiz {
    @Resource
    private TUserAdressMapper tUserAdressMapper;

    @Autowired
    private CacheUtil cacheUtil;

    public void addUserAddress(TUserAdress tUserAdress){
        TUser userSession = (TUser) cacheUtil.getUserSession();
        tUserAdress.setUserId(userSession.getUserId());
        tUserAdress.setCreateTime(DateUtil.getDateTime3());
        tUserAdress.setUserAddressId(SequenceUtil.nextId());
        if(tUserAdress.getStatus()==null){
            tUserAdress.setStatus(0);
        }
        if(tUserAdress.getStatus().intValue()==1){
            TUserAdressExample tUserAdressExample = new TUserAdressExample();
            tUserAdressExample.createCriteria().andUserIdEqualTo(userSession.getUserId());
            List<TUserAdress> tUserAdresses = tUserAdressMapper.selectByExample(tUserAdressExample);
            if(tUserAdresses!=null && tUserAdresses.size()>0){
                for(TUserAdress updateUserAddress : tUserAdresses){
                    updateUserAddress.setStatus(0);
                    tUserAdressMapper.updateByPrimaryKeySelective(updateUserAddress);
                }
            }
        }
        tUserAdressMapper.insert(tUserAdress);
    }
}
